package com.k12.app.interceptors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TenantInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String tenantId = request.getHeader("X-TenantID");
        if(StringUtils.isBlank(tenantId)){
            response.setContentType("application/json;charset=UTF-8");
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            response.getWriter().write(
                    "{\"code\": 401, \"msg\": \"未授权访问，TENANT_ID不能为空\"}"
            );
            return false;
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
